# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Sergey Karayev
# --------------------------------------------------------

cimport cython
import numpy as np
cimport numpy as np

DTYPE = np.float
ctypedef np.float_t DTYPE_t

def bbox_overlaps(
        np.ndarray[DTYPE_t, ndim=2] boxes,
        np.ndarray[DTYPE_t, ndim=2] query_boxes):
    """
    Parameters
    ----------
    boxes: (N, 5) ndarray of float (batch_id, x1, y1, x2, y2)
    query_boxes: (K, 5) ndarray of float (batch_id, x1, y1, x2, y2)
    Returns
    -------
    overlaps: (N, K) ndarray of overlap between boxes and query_boxes
    """
    cdef unsigned int N = boxes.shape[0]
    cdef unsigned int K = query_boxes.shape[0]
    cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
    cdef DTYPE_t iw, ih, box_area
    cdef DTYPE_t ua
    cdef unsigned int k, n
    for k in range(K):
        box_area = (
            (query_boxes[k, 2+1] - query_boxes[k, 0+1] + 1) *
            (query_boxes[k, 3+1] - query_boxes[k, 1+1] + 1)
        )
        for n in range(N):
            if query_boxes[k, 0] == boxes[n, 0]:
                iw = (
                    min(boxes[n, 2+1], query_boxes[k, 2+1]) -
                    max(boxes[n, 0+1], query_boxes[k, 0+1]) + 1
                )
                if iw > 0:
                    ih = (
                        min(boxes[n, 3+1], query_boxes[k, 3+1]) -
                        max(boxes[n, 1+1], query_boxes[k, 1+1]) + 1
                    )
                    if ih > 0:
                        ua = float(
                            (boxes[n, 2+1] - boxes[n, 0+1] + 1) *
                            (boxes[n, 3+1] - boxes[n, 1+1] + 1) +
                            box_area - iw * ih
                        )
                        overlaps[n, k] = iw * ih / ua
    return overlaps
